<?php
if (get_option("hfr_setup") != 1 || $act == "edit") {
	$status = 'check_customer';
	$hfr_customer = get_option("hfr_customer");
	$hfr_license_key = get_option("hfr_license_key");
	$hfr_blog_url = get_option("hfr_blog_url");
	$hfr_blog_username = get_option("hfr_blog_username");
	$hfr_blog_password = get_option("hfr_blog_password");
	
	//Check valid email
	if ($_POST['status'] == 'check_customer') {
		$status = 'check_customer';
		$hfr_customer = $_POST['hfr_customer'];
		$hfr_blog_url = $_POST['hfr_blog_url'];
		$hfr_blog_username = $_POST['hfr_blog_username'];
		$hfr_blog_password = $_POST['hfr_blog_password'];
		
		//Connect to server hatforrent and check status
		$url = HFR_SERVER . "/rest/check-connection?c=".$hfr_customer."&s=".$status."&b=".$hfr_blog_url."&u=".$hfr_blog_username."&p=".$hfr_blog_password;
		
		$content = hfr_remote_get($url);
		
		if ($content != null && $content != "") {
			$json = json_decode($content);
			if (isset($json) && isset($json->status)) {
				if ($json->status == "success") {
					$status = 'check_license';
					$success = $json->message;
					
					update_option("hfr_customer", $hfr_customer);
					update_option("hfr_blog_url", $hfr_blog_url);
					update_option("hfr_blog_username", $hfr_blog_username);
					update_option("hfr_blog_password", $hfr_blog_password);
					
				} else {
					$error = $json->message;
				}
			} else {
				$error = "Can't connection to ".HFR_SERVER.". Please check again email.";
			}
		} else {
			$error = "Can't connection to ".HFR_SERVER.".";
		}
		
	}
	
	
	if ($_POST['status'] == 'check_license') {
		$status = $_POST['status'];
		$hfr_customer = $_POST['hfr_customer'];
		$hfr_blog_url = $_POST['hfr_blog_url'];
		$hfr_license_key = $_POST['hfr_license_key'];
		
		$url = HFR_SERVER . "/rest/check-connection?c=".$hfr_customer."&s=".$status."&b=".$hfr_blog_url."&k=".urlencode($hfr_license_key);
		
		$content = hfr_remote_get($url);
		if ($content != null && $content != "") {
			$json = json_decode($content);
			
			if (isset($json) && isset($json->status)) {
				if ($json->status == "success") {
					$status = 'success';
					$success = $json->message;
					$expired = $json->expired;
					update_option("hfr_license_key", $hfr_license_key);
					update_option("hfr_setup", 1);
					update_option("hfr_expired", $expired);
					
					//Redirect to page
					$redirect = "admin.php?page=hfr_setup"; 
					header("Location: ".$redirect);
					exit();
				} else {
					$error = $json->message;
				}
			} else {
				$error = "Can't connection to ".HFR_SERVER.". Please check again email.";
			}
		} else {
			$error = "Can't connection to ".HFR_SERVER.".";
		}
	}
	
	//Set button name
	$btn_submit = "Check Infomation";
	
	if ($status == "check_license") {
		$btn_submit = "Check License";
	}
?>
<div class="hfr-body">
	<?php echo "<h2 class='title'>". __("Setup Connection to Hatforrent", "hfr_dom") . "</h2>";?>
    
    <?php
    if (isset($success)) {
    ?>
    <div class="hfr-alert-success"><?php echo $success?></div>
    <?php
    }
    if (isset($error)) {
    ?>
    <div class="hfr-alert-danger"><?php echo $error?></div>
    <?php
    }
    if (isset($info)) {
    ?>
    <div class="hfr-alert-info"><?php echo $info?></div>
    <?php 
    }
    ?>
    
    <div class="hft-setup-account clearafter">
        <form name="hfr_form" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
        	<input type="hidden" name="status" value="<?php echo $status?>">
            <table class="form-table">
                <tbody>
                    <tr>
                        <th><label for="hfr_email">Gmail Address</label></th>
                        <td>
                            <input type="hfr_email" class="regular-text" value="<?php echo $hfr_customer; ?>" id="hfr_customer" name="hfr_customer" required <?php echo $status == "check_license" ? "readonly=\"readonly\"" : "" ?> >
                            <br />
                            <span class="description">Ex: your_email@gmail.com</span>
                            <a href="#" class="hfr-forgot-license">Send license to this email</a>
                        </td>
                    </tr>
                    
                    <tr>
                        <th><label for="hfr_blog_url">Blog Url <span class="description">(required)</span></label></th>
                        <td>
                            <input type="url" class="regular-text" value="<?php echo $hfr_blog_url?>" id="hfr_blog_url" name="hfr_blog_url" placeholder="Blog's Url" required <?php echo $status == "check_license" ? "readonly=\"readonly\"" : "" ?>>
                            <br />
                            <span class="description">Ex: http://www.yourwebsite.com</span>
                        </td>
                    </tr>
                    
                    <?php 
                    if ($status == "check_customer") {
                    ?>
                    <tr>
                        <th><label for="hfr_blog_username">Blog's Username <span class="description">(required)</span></label></th>
                        <td>
                            <input type="text" class="regular-text" value="<?php echo $hfr_blog_username?>" id="hfr_blog_username" name="hfr_blog_username" placeholder="Blog's Username" required>
                        </td>
                    </tr>
                    
                    <tr>
                        <th><label for="hfr_blog_password">Blog's Password <span class="description">(required)</span></label></th>
                        <td>
                            <input type="password" class="regular-text" value="<?php echo $hfr_blog_password?>" id="hfr_blog_password" name="hfr_blog_password" placeholder="Blog's Password" required>
                        </td>
                    </tr>
                    <?php
                    } 
                    ?>
                    
                    <?php 
                    if ($status == "check_license") {
                    ?>
                    <tr>
                        <th><label for="hfr_license_key">License Key</label></th>
                        <td>
                            <input type="text" class="regular-text" value="<?php echo $hfr_license_key; ?>" id="hfr_license_key" name="hfr_license_key" required >
                            <br />
                            <span class="description">Ex: xyz01233312461131ABC</span>
                            <a href="#" class="hfr-forgot-license">Send license to this email</a>
                        </td>
                    </tr>
                    <?php	
                    }
                    ?>
                </tbody>
            </table>		
            <p class="submit">
                <input type="submit" name="Submit" value="<?php _e($btn_submit, 'hfr_trdom' ) ?>" class="hfr-btn-blue" />
            </p>
        </form>
    </div>
</div>
<?php 
} else {
	$hfr_customer = get_option("hfr_customer");
	$hfr_license_key = get_option("hfr_license_key");
	$hfr_blog_url = get_option("hfr_blog_url");
	$hfr_blog_username = get_option("hfr_blog_username");
	$hfr_blog_password = get_option("hfr_blog_password");
	$hfr_expired = get_option("hfr_expired");
?>
<div class="hfr-body">
	<h2 class="title">Account Information</h2>
	<div class="hfr-account-info">
    	<div><span>Email Address</span> <?php echo $hfr_customer?></div>
        <div><span>License Key</span> <?php echo $hfr_license_key?></div>
        <div><span>Expired date</span> <?php echo $hfr_expired ?></div>
        <div><span>Server Url</span> <?php echo $hfr_blog_url?></div>
        <div><span>Username</span> <?php echo $hfr_blog_username?></div>        
        <a class="hfr-btn-blue" href="admin.php?page=hfr_setup&act=edit">Change Information</a>
    </div>
</div>
<?php
}
?>